<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      chartInstance: null,
      allData: null, // 服务器返回的数据
      startValue: 0, // 区域缩放的起点值
      endValue: 9 // 区域缩放的终点值
    }
  },
  // DOM 结构加载完成后执行
  mounted () {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    // 页面加载完成后，主动进行屏幕适配
    this.screenAdapter()
  },
  destroyed () {
    // 页面销毁时，取消监听器
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timer)
  },
  methods: {
    // 初始化实例对象
    initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.rank_ref, 'chalk')
      const option = {
        title: {
          text: '▌ 地区销量排行',
          left: 20,
          top: 20
        },
        tooltip: {
          show: true
        },
        grid: {
          top: '40%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            type: 'bar'
          }
        ]
      }
      this.chartInstance.setOption(option)
      // 监听鼠标移入事件
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timer)
      })
      this.chartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    // 获取服务器数据
    async getData () {
      const { data: res } = await this.$http.get('rank')
      // console.log(res)
      this.allData = res
      // 从大到小排序
      this.allData.sort((a, b) => {
        return b.value - a.value
      })
      // 更新图表
      this.updateChart()
      // 启动定时器
      this.startInterval()
    },
    // 更新图表
    updateChart () {
      const colorArr = [
        ['#0BA82C', '#4FF778'],
        ['#2E72BF', '#23E5E5'],
        ['#5052EE', '#AB6EE5']
      ]
      // 获取x轴 - 省份
      const provinceArr = this.allData.map(item => {
        return item.name
      })
      // 获取y轴 - 销售额
      const valueArr = this.allData.map(item => {
        return item.value
      })
      // 设置 option 数据
      const option = {
        xAxis: {
          data: provinceArr
        },
        // 区域缩放
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: arg => {
                let targetColorArr = null
                // 不同数值显示不同颜色效果
                if (arg.value > 300) {
                  targetColorArr = colorArr[0]
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1]
                } else {
                  targetColorArr = colorArr[2]
                }
                // LinearGradient 颜色渐变对象
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0]
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1]
                  }
                ])
              }
            }
          }
        ]
      }
      this.chartInstance.setOption(option)
    },
    // 当浏览器窗口发生变化时，监听的函数
    screenAdapter () {
      // 和分辨率大小相关的配置项
      const adapterOption = {}
      this.chartInstance.setOption(adapterOption)
      // 手动调用图表的resize() 才能生效
      this.chartInstance.resize()
    },
    // 定时器
    startInterval () {
      if (this.timer) {
        clearInterval(this.timer)
      }
      this.timer = setInterval(() => {
        this.startValue++
        this.endValue++
        if (this.endValue > this.allData.length - 1) {
          this.endValue = 0
          this.startValue = 9
        }
        this.updateChart()
      }, 2000)
    }
  }
}
</script>

<style lang="less" scoped></style>
